//
//  AccountChangeListController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/4/26.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "AccountChangeListController.h"
#import "AccountChangeModel.h"
#import "AccountChangeCell.h"

@interface AccountChangeListController ()<UITableViewDelegate,UITableViewDataSource>
{
     NSUInteger _refreshIndex;
    BOOL _judgeList;
    UIImageView *noDrawImage;
}
@property (nonatomic,strong)NSArray *accountListArr;
@property(nonatomic,strong)UITableView *accountListView;
@property(nonatomic,strong)UIButton *selectBtn;
@property(nonatomic,strong)MBProgressHUD *progressHUD;
@property(nonatomic,strong)NSMutableArray *accountListArray;

@end

@implementation AccountChangeListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self buildView];
    
    _refreshIndex = 1;
    _judgeList = true;
    [self addPullUpdateData];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self loadAccountChangeLishData:@{@"username":USERNAME,
                                      @"amount_type":@"1",
                                      @"num":@"15"}];
    
    if ([self.accountListView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.accountListView setSeparatorInset:UIEdgeInsetsMake(0, -25, 0, 0)];
        
    }
}

- (void)accountChooseDidClick:(UIButton *)button
{
    _selectBtn.enabled = true;
    button.enabled = false;
    _selectBtn = button;
    [noDrawImage removeFromSuperview];
    [self.accountListView.mj_footer resetNoMoreData];
    [self.accountListArray removeAllObjects];
    
    if (button.tag == 1010) {
        _judgeList = YES;
  
        [self loadAccountChangeLishData:@{@"username":USERNAME,
                                          @"amount_type":@"1",
                                          @"num":@"15",
                                          }];
    }else{
        _judgeList = false;
        [self loadAccountChangeLishData:@{@"username":USERNAME,
                                          @"amount_type":@"2",
                                          @"num":@"15",
                                          }];


    }
    
}


#pragma mark >>>>>>>>刷新
- (void)addPullUpdateData
{
    self.accountListView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(loadAccountChangeMoreListData)];
    
}


- (void)loadAccountChangeMoreListData
{
    _refreshIndex ++;
    NSString *indexStr = [NSString stringWithFormat:@"%zd",_refreshIndex];
   
    if (_judgeList) {
    
        [self loadAccountChangeLishData:@{@"username":USERNAME,
                                          @"amount_type":@"1",
                                          @"num":@"15",
                                          @"page":indexStr
                                          }];

    }else{
        
        [self loadAccountChangeLishData:@{@"username":USERNAME,
                                          @"amount_type":@"2",
                                          @"num":@"15",
                                          @"page":indexStr
                                          }];

    }

    [self.accountListView.mj_footer endRefreshing];
}

- (void)loadAccountChangeLishData:(NSDictionary *)params
{
    _progressHUD = [MBProgressHUD show:self.view andMessage:nil];
    [self.view addSubview:_progressHUD];
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/bankroll/bankrollLog" completeBlock:^(id object) {
        
        if (JudgeBackDataCode) {
            
            NSArray *arr = [object objectForKey:@"data"];
            for (NSDictionary *dict in arr) {
                AccountChangeModel *accountModel = [[AccountChangeModel alloc] initWithDictionary:dict error:nil];
                if (accountModel) {
                    [self.accountListArray addObject:accountModel];
                }
                
            }
            
        }else{
            
            if (_refreshIndex == 1) {
                
                noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_NoRecord"]];
                [self.view addSubview:noDrawImage];
                
                [noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(self.view.mas_centerX);
                    make.centerY.equalTo(self.view.mas_centerY).offset(-HEIGHT/8);
                    make.width.mas_equalTo(WIDTH/1.5);
                    make.height.mas_equalTo(WIDTH/2.4);
                }];
                
            }else{
                
                 [self.accountListView.mj_footer endRefreshingWithNoMoreData];
            }
           
           
        }
        
        
        [_progressHUD hide:YES];
        [self.accountListView reloadData];
    }];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.accountListArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     AccountChangeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountChangeCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    AccountChangeModel *accountModel = self.accountListArray[indexPath.section];
    cell.userLbl.text = accountModel.username;
    cell.descLbl.text = accountModel.details;
    cell.numLbl.text = accountModel.funds_chang_amunt;
    cell.timeLbl.text = accountModel.add_time;
    
    return cell;
}

#pragma mark buildView
- (void)buildView
{
    self.title = @"账户转换记录";
    [self.view addSubview:self.accountListView];
    self.view.backgroundColor = BACKGROUND_COLOR;
    
    for (int i = 0; i < self.accountListArr.count; ++i) {
        
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:[NSString stringWithFormat:@"%@",self.accountListArr[i]] forState:UIControlStateNormal];
        btn.titleLabel.font = FONT(15);
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:DEFAULT_COLOR forState:UIControlStateDisabled];
        btn.frame = CGRectMake((WIDTH/2+0.5)*i,0,WIDTH/2-0.5,50);
        [self.view addSubview:btn];
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn addTarget:self action:@selector(accountChooseDidClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 1010+i;
        if (btn.tag == 1010) {
            btn.enabled = false;
            _selectBtn = btn;
        }
    }
    //黑线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(WIDTH/2-0.5,0,1,50)];
    lineView.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:lineView];
    
}

- (UITableView *)accountListView
{
    if (!_accountListView) {
        _accountListView = [[UITableView alloc] initWithFrame:CGRectMake(0,51,WIDTH,HEIGHT-50-64) style:UITableViewStylePlain];
        _accountListView.backgroundColor = BACKGROUND_COLOR;
        _accountListView.delegate =self;
        _accountListView.dataSource =self;
        _accountListView.rowHeight = 50;
        _accountListView.tableFooterView = [[UIView alloc] init];
        
        [_accountListView registerClass:[AccountChangeCell class] forCellReuseIdentifier:@"AccountChangeCell"];
    }
    return _accountListView;
}

- (NSArray *)accountListArr {
    if (!_accountListArr) {
        _accountListArr = @[@"充值账户",@"营业账户"];
    }
    return _accountListArr;
}

- (NSMutableArray *)accountListArray
{
    if (!_accountListArray) {
        _accountListArray = [NSMutableArray array];
    }return _accountListArray;
}

@end
